package com.nineclock.common.exception;

import com.nineclock.common.entity.Result;
import com.nineclock.common.enums.ResponseEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 全局异常处理器
 */
@ResponseBody //以json形式返回异常信息
@ControllerAdvice // 开启异常增强
@Slf4j
public class ExceptionHandler {
    // 捕获其他异常
    @org.springframework.web.bind.annotation.ExceptionHandler(Exception.class)
    public Result ex(Exception e) {
        log.error(e.getMessage());
        return Result.errorCodeMessage(ResponseEnum.SERVER_ERROR.getCode(), ResponseEnum.SERVER_ERROR.getMessage());
    }

    // 捕获自定义异常
    @org.springframework.web.bind.annotation.ExceptionHandler(NCException.class)
    public Result nc(NCException e) {
        log.error(e.getMessage());
        return Result.errorCodeMessage(e.getCode(), e.getMessage());
    }
}
